package com.canaan.business.service.storehouse.impl;

import java.util.Date;
import java.util.List;

import com.canaan.business.common.enums.inOutStock.InOutStockTypeEnum;
import com.canaan.business.common.enums.storehouse.InOutStorageOperationEnum;
import com.canaan.business.common.enums.storehouse.StorehouseStockStatusEnum;
import com.canaan.business.domain.storehouse.WmsBarcodeRecord;
import com.canaan.business.domain.storehouse.WmsInOutStorage;
import com.canaan.business.domain.storehouse.WmsStorehouseStock;
import com.canaan.business.mapper.storehouse.WmsInOutStorageMapper;
import com.canaan.business.mapper.storehouse.WmsStorehouseStockMapper;
import com.canaan.business.service.storehouse.IWmsBarcodeRecordService;
import com.canaan.common.exception.ServiceException;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.storehouse.MergePackMapper;
import com.canaan.business.domain.storehouse.MergePack;
import com.canaan.business.service.storehouse.IMergePackService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-06
 */
@Service
public class MergePackServiceImpl implements IMergePackService {
    @Resource
    private MergePackMapper mergePackMapper;

    @Resource
    private WmsStorehouseStockMapper wmsStorehouseStockMapper;

    @Resource
    private IWmsBarcodeRecordService wmsBarcodeRecordService;

    @Resource
    private WmsInOutStorageMapper inOutStorageMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public MergePack selectMergePackById(Long id) {
        return mergePackMapper.selectMergePackById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param mergePack 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<MergePack> selectMergePackList(MergePack mergePack) {
        return mergePackMapper.selectMergePackList(mergePack);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param mergePack 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertMergePack(MergePack mergePack) {
        if(StringUtils.isBlank(mergePack.getNo())){
            mergePack.setNo(TableNoUtil.getNo(MergePack.class));
        }

        WmsStorehouseStock sourceStock = this.getInfoByBarcode(mergePack.getSourceBarcode(), "来源条码");
        WmsStorehouseStock targetStock = this.getInfoByBarcode(mergePack.getTargetBarcode(), "目标条码");

        if (!sourceStock.getStorehouseLocationNo().equals(targetStock.getStorehouseLocationNo())){
            throw new ServiceException("来源条码与目标条码库位不一致，不能合箱!");
        }

        WmsBarcodeRecord barcodeRecord = new WmsBarcodeRecord();
        barcodeRecord.setSupplierName(targetStock.getSupplierName());
        barcodeRecord.setProductionDate(targetStock.getProductionDate());
        barcodeRecord.setEffectiveDate(targetStock.getEffectiveDate());
        barcodeRecord.setValidDateNum(targetStock.getValidDateNum());
        barcodeRecord.setBatchNo(targetStock.getBatchNo());
        barcodeRecord.setOrderNo(targetStock.getOrderNo());
        barcodeRecord.setProductName(targetStock.getProductName());
        barcodeRecord.setProductNo(targetStock.getProductNo());
        barcodeRecord.setQuantity(mergePack.getSourceBoxNum());
        wmsBarcodeRecordService.insertWmsBarcodeRecord(barcodeRecord);

        sourceStock.setQty(0L);
        sourceStock.setDelFlag("1");
        wmsStorehouseStockMapper.updateWmsStorehouseStock(sourceStock);
        targetStock.setQty(mergePack.getSourceBoxNum() + mergePack.getTargetBoxNum());
        wmsStorehouseStockMapper.updateWmsStorehouseStock(targetStock);

        WmsInOutStorage inOutStorage = new WmsInOutStorage();
        inOutStorage.setCreateBy(SecurityUtils.getUsername());
        inOutStorage.setCreateTime(DateUtils.getNowDate());
        inOutStorage.setAreaNo(sourceStock.getStorehouseAreaNo());
        inOutStorage.setBatchNo(sourceStock.getBatchNo());
        inOutStorage.setOperation(InOutStorageOperationEnum.OUT.getCode());
        inOutStorage.setProductName(barcodeRecord.getProductName());
        inOutStorage.setProductNo(barcodeRecord.getProductNo());
        inOutStorage.setProductUnit(sourceStock.getCompany());
        inOutStorage.setQuantity(mergePack.getSourceBoxNum());
        inOutStorage.setRelNo(mergePack.getNo());
        inOutStorage.setStorehouseLocationNo(sourceStock.getStorehouseLocationNo());
        inOutStorage.setStorehouseNo(sourceStock.getStorehouseNo());
        inOutStorage.setType(InOutStockTypeEnum.MERGE.getCode().longValue());
        inOutStorage.setNo(TableNoUtil.getNo(WmsInOutStorage.class));
        inOutStorage.setBarcode(sourceStock.getBarcode());
        inOutStorageMapper.insertWmsInOutStorage(inOutStorage);
        inOutStorage.setOperation(InOutStorageOperationEnum.IN.getCode());
        inOutStorage.setBatchNo(targetStock.getBatchNo());
        inOutStorage.setAreaNo(targetStock.getStorehouseAreaNo());
        inOutStorage.setStorehouseLocationNo(targetStock.getStorehouseLocationNo());
        inOutStorage.setNo(TableNoUtil.getNo(WmsInOutStorage.class));
        inOutStorage.setBarcode(targetStock.getBarcode());
        inOutStorageMapper.insertWmsInOutStorage(inOutStorage);

        mergePack.setCreateBy(SecurityUtils.getUsername());
        mergePack.setCreateTime(DateUtils.getNowDate());
        mergePack.setSumNum(targetStock.getQty());
        return mergePackMapper.insertMergePack(mergePack);
    }

    /**
     * 批量新增【请填写功能名称】
     *
     * @param mergePackList 【请填写功能名称】列表
     * @return 结果
     */
    @Override
    public int batchInsertMergePack(List<MergePack> mergePackList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (MergePack mergePack : mergePackList){
            if(StringUtils.isBlank(mergePack.getNo())){
                mergePack.setNo(TableNoUtil.getNo(MergePack.class));
            }
            mergePack.setCreateBy(username);
            mergePack.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<mergePackList.size();) {
            int endIndex = i+500;
            if (endIndex > mergePackList.size()) {
                endIndex = mergePackList.size();
            }
            insertNum = insertNum + mergePackMapper.batchInsertMergePack(mergePackList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param mergePack 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateMergePack(MergePack mergePack) {
        mergePack.setUpdateBy(SecurityUtils.getUsername());
        mergePack.setUpdateTime(DateUtils.getNowDate());
        return mergePackMapper.updateMergePack(mergePack);
    }

    /**
     * 批量修改【请填写功能名称】
     *
     * @param mergePackList 【请填写功能名称】列表
     * @return 结果
     */
    @Override
    public int batchUpdateMergePack(List<MergePack> mergePackList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (MergePack mergePack : mergePackList){
            mergePack.setUpdateBy(username);
            mergePack.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<mergePackList.size();) {
            int endIndex = i+500;
            if (endIndex > mergePackList.size()) {
                endIndex = mergePackList.size();
            }
            updateNum = updateNum + mergePackMapper.batchUpdateMergePack(mergePackList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键集合
     * @return 结果
     */
    @Override
    public int deleteMergePackByIds(Long[] ids) {
        return mergePackMapper.deleteMergePackByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteMergePackById(Long id) {
        return mergePackMapper.deleteMergePackById(id);
    }

    /**
     * 查询【请填写功能名称】
     *
     * @param no 编号
     * @return 【请填写功能名称】
     */
    @Override
    public MergePack selectMergePackByNo(String no) {
        return mergePackMapper.selectMergePackByNo(no);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteMergePackByNos(String[] nos) {
        return mergePackMapper.deleteMergePackByNos(nos);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteMergePackByNo(String no) {
        return mergePackMapper.deleteMergePackByNo(no);
    }

    private WmsStorehouseStock getInfoByBarcode(String barcode, String barcodeTag)
    {
        WmsStorehouseStock stock = new WmsStorehouseStock();
        stock.setBarcode(barcode);
        List<WmsStorehouseStock> list = wmsStorehouseStockMapper.selectWmsStorehouseStockList(stock);
        if(list.isEmpty()){
            throw new ServiceException(barcodeTag+"不存在");
        }
        stock = list.get(0);
        if(StorehouseStockStatusEnum.FREEZE.getCode().equals(stock.getStatus())){
            throw new ServiceException(barcodeTag+"库存已被冻结");
        }else if(StorehouseStockStatusEnum.LOCK.getCode().equals(stock.getStatus())){
            throw new ServiceException(barcodeTag+"库存已被锁定");
        }
        return stock;
    }
}
